package com.moonshine.parser.models;

public abstract class AS3Signature {

	public String name;
	public int flags = 0;
	public int access = Flags.ACCESS_DEFAULT;
	public int line = 0;
	public int col = 0;
	public int charIndex = 0;

	static public void appendAccess(StringBuilder sb, int access) {
		appendAccess(sb, access, true);
	}

	public static Boolean checkFlag(int flags, int flag) {
		return (int) (flags & flag) > 0;
	}

	static public void appendAccess(StringBuilder sb, int access, Boolean pretty) {
		if (checkFlag(access, Flags.ACCESS_INTERNAL)) {
			sb.append(pretty ? "internal" : "i");
		}
		if (checkFlag(access, Flags.VISIBLITIY_STATIC)) {
			sb.append(pretty ? "static" : "s");
		}
		if (checkFlag(access, Flags.VISIBLITIY_NATIVE)) {
			sb.append(pretty ? "native" : "n");
		}
		if (checkFlag(access, Flags.VISIBLITIY_OVERRIDE)) {
			sb.append(pretty ? "override " : "o");
		}
		if (checkFlag(access, Flags.ACCESS_PUBLIC)) {
			sb.append(pretty ? "public " : "p");
		}
		if (checkFlag(access, Flags.ACCESS_PROTECTED)) {
			sb.append(pretty ? "protected " : "d");
		}
		if (checkFlag(access, Flags.ACCESS_PRIVATE)) {
			sb.append(pretty ? "private " : "v");
		}

		if (checkFlag(access, Flags.VISIBLITIY_GETTER)) {
			sb.append(pretty ? "get " : "g");
		} else if (checkFlag(access, Flags.VISIBLITIY_SETTER)) {
			sb.append(pretty ? "set " : "s");
		}

	}

	public String toString(Boolean pretty, Boolean omitIndices) {
		final StringBuilder sb = new StringBuilder();
		AS3Signature.appendAccess(sb, access, pretty);
		return sb.toString();
	}

	public String toString(Boolean pretty) {
		return toString(pretty, false);
	}

	public String toString() {
		return toString(true);

	}

	public static void appendIndices(StringBuilder sb, AS3Signature sig) {
		// TODO Auto-generated method stub
		appendIndices(sb, sig, true);
	}

	public static void appendIndices(StringBuilder sb, AS3Signature sig,
			Boolean pretty) {
		// TODO Auto-generated method stub
		final String joinChar = pretty ? ":" : " ";
		sb
				.append(String.valueOf(sig.charIndex) + joinChar
						+ String.valueOf(sig.line) + joinChar
						+ String.valueOf(sig.col));
	}

}
